const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const packageNames = {
  test: "warnin_admin_test",
  gray: "warnin_admin_gray",
  gray1: "warnin_admin_gray",
  prod: "warnin_admin_prod",
  production: "warnin_admin_prod",
}

module.exports = {
  // webpack 配置
  webpack: {
    // 配置别名
    alias: {
      // 约定：使用 @ 表示 src 文件所在路径
      '@': path.resolve(__dirname, 'src')
    },
    configure: (webpackConfig, { env, paths }) => {
      // console.log(env)
      // console.log(process.env)
      console.log(process.env.PROXY_ENV_XP)
      if (env === 'production') {
        const key = process.env.CMDEFINE_ENV || 'production'
        paths.appBuild = packageNames[key]
        webpackConfig.output = {
          ...webpackConfig.output,
          path: path.resolve(__dirname, packageNames[key]),
          publicPath: './'
        }
      }
      return webpackConfig
    },
    //配置代理解决跨域
    // devServer: {
    //   proxy: {
    //     "/api": {
    //       target: process.env.PROXY_ENV,
    //       changeOrigin: true,
    //       pathRewrite: {
    //         '^/api': '/'
    //       }
    //     }
    //   }
    // },
    plugins: [
      new TerserPlugin({
        sourceMap: true, // Must be set to true if using source-maps in production
        terserOptions: {
          ecma: undefined,
          warnings: false,
          parse: {},
          compress: {
            drop_console: process.env.NODE_ENV === "production", // 生产环境下移除控制台所有的内容
            drop_debugger: false, // 移除断点
            pure_funcs:
              process.env.NODE_ENV === "production" ? ["console.log"] : "", // 生产环境下移除console
          },
        },
      }),
    ]
  }
}